package org.example.petadoption.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.petadoption.entity.DashboardDTO;
import org.example.petadoption.entity.Merchant;
import org.example.petadoption.entity.ResultBody;
import org.example.petadoption.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 商家信息表 前端控制器
 * </p>
 *
 * @author PetAdoption
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController {

    @Autowired
    IMerchantService merchantService;

    //商家资质未审核列表
    @GetMapping("/selAccreditation")
    public ResultBody selAccreditation() {
        LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<>();
        int key = 0;
        queryWrapper.eq(Merchant::getStatus,key);
        List<Merchant> merchants = merchantService.list(queryWrapper);
        return ResultBody.success("未审核的商家",merchants);
    }

    //商家资质上传
    @PostMapping("/upAccreditation")
    public ResultBody accreditation(@RequestBody Merchant merchant) {

        if (merchant.getStatus() == null) {
            merchant.setStatus((byte) 0);
        }
        if (merchant.getUserId() <= 0){
            ResultBody.fail("用户id不合法");
        }
        if (StringUtils.isBlank(merchant.getName())) {
            ResultBody.fail("店铺名不能为空");
        }
        if (StringUtils.isEmpty(merchant.getContactPhone())){
            ResultBody.fail("联系电话不能为空");
        }
        if (StringUtils.isEmpty(merchant.getContactPerson())){
            ResultBody.fail("联系人不能为空");
        }
        if (StringUtils.isEmpty(merchant.getCategory())){
            ResultBody.fail("主营分类不能为空");
        }
        if (StringUtils.isEmpty(merchant.getAddress())){
            ResultBody.fail("经营地址不能为空");
        }
        if (StringUtils.isEmpty(merchant.getLicense())){
            ResultBody.fail("营业职照url不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCardFront())){
            ResultBody.fail("身份证正面照不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCardBack())){
            ResultBody.fail("身份证反面照不能为空");
        }
        if (StringUtils.isEmpty(merchant.getIdCard())){
            ResultBody.fail("身份证号不能为空");
        }

        boolean result = merchantService.save(merchant);
        return result?ResultBody.success("",result):ResultBody.fail();

    }

    //根据id审核商家，key字段为审核状态：0未审核 1批准通过 2不批准通过
    @PostMapping("/setAccreditation")
    public ResultBody setAccreditation(int id, int key) {
        if (id <= 0) {
            ResultBody.fail("商家id不合法");
        }
        LambdaUpdateWrapper<Merchant> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(Merchant::getId, id).eq(Merchant::getStatus, (byte) key);
        boolean result = merchantService.update(queryWrapper);
        return result?ResultBody.success("审批通过",result):ResultBody.fail("审核失败");
    }

    //管理员获取数据统计
    @GetMapping("/dashboard")
    public ResultBody getDashboard() {
        DashboardDTO stats = merchantService.getDashboardStats();
        return ResultBody.success("查询成功", stats);
    }



}
